using System;
using Evaluant.OPath.Expressions.Collections;

namespace Evaluant.OPath.Expressions
{
	[Serializable]
    public class Call : Expression, ICloneable
	{ 
		/// <summary>
		/// Creates a new <see cref="Call"/> instance.
		/// </summary>
		public Call() : base()
		{
			operands = new ConstraintCollection();
			name = String.Empty;
		}

        public Call(string name, ConstraintCollection operands)
        {
            this.name = name;
            this.operands = operands;
        }

        [System.Diagnostics.DebuggerStepThrough]
        public override void Accept(OPathVisitor visitor)
		{
			visitor.Visit(this);
		}

		private ConstraintCollection operands;
		/// <summary>
		/// Gets the constraints.
		/// </summary>
		/// <value></value>
		public ConstraintCollection Operands
		{
			get { return operands; }
		}

		private string name;
		/// <summary>
		/// Gets or sets the name.
		/// </summary>
		/// <value></value>
		public string Name
		{
			get { return name; }
			set { name = value; }
		}

        public override void ToString(System.Text.StringBuilder sb)
        {
            sb.Append(name).Append("(");

            if (operands.Count > 0)
            {
                operands[0].ToString(sb);
            }

            for (int i = 1; i < operands.Count; i++)
            {
                sb.Append(", ");
                operands[i].ToString(sb);
            }

            sb.Append(")");
        }

        #region Membres de ICloneable

        public override object Clone()
		{
			Call clone = new Call();

			clone.Name = this.Name;
			foreach(Constraint constraint in this.Operands)
				clone.Operands.Add((Constraint)constraint.Clone());

			return clone;
		}

		#endregion
	}
}
